﻿var dataList = [], removeList = [], addList = [];
var vehicleid = GetQueryStr("vehicleId");
var isAdd = false;
var thistoday = "";

$(document).ready(function () {
    $('#calendar').fullCalendar({
        header: {
            left: 'myprevbutton today',
            center: 'title',
            right: 'mynextbutton'
        },
        monthNames: ["一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月"],
        monthNamesShort: ["一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月"],
        dayNames: ["周日", "周一", "周二", "周三", "周四", "周五", "周六"],
        dayNamesShort: ["周日", "周一", "周二", "周三", "周四", "周五", "周六"],
        today: ["今天"],
        firstDay: 1,
        buttonText: {
            today: '今天',
            month: '月',
            week: '周',
            day: '日',
            prev: '上一月',
            next: '下一月'
        },
        customButtons: {
            myprevbutton: {
                text: '上一月',
                click: function (e) {
                    $(e).addClass("fc-state-disabled");
                    var isnext = nextToMonth(".fc-myprevbutton-button", ".fc-mynextbutton-button", 0, "prev");
                    if (isnext) { $('#calendar').fullCalendar('prev'); }
                }
            },
            mynextbutton: {
                text: '下一月',
                click: function () {
                    var isnext = nextToMonth(".fc-mynextbutton-button", ".fc-myprevbutton-button", 3, "next");
                    if (isnext) { $('#calendar').fullCalendar('next'); }
                }
            }
        },
        eventLimit: true,
        events: function (start, end, timezone, callback) {
            var sTime = start.format("YYYY/MM/DD");
            var eTime = end.format("YYYY/MM/DD");

            jQuery.loadHtml('/pgajax.axd?GET_SCHEDULE=ok&STIME=' + sTime + '&ETIME=' + eTime + '&VEHICLEID=' + vehicleid + '').done(function (_rets) {
                _rets = JSON.parse(_rets);
                if (_rets.records <= 0) { return; }
                var events = [];
                $(_rets.rows).each(function (i, e) {
                    if (e.VEHICLE_INFO_ID == vehicleid) {
                        var obj = new Object();
                        obj.id = e.VEHICLE_SCHEDULE_ID;
                        obj.title = e.SCHEDULE_TITLE;
                        obj.description = e.SCHEDULE_DESCRIPTION;
                        obj.start = e.START_TIME;
                        obj.end = e.END_TIME;
                        obj.color = e.SCHEDULE_COLOR;
                        obj.allDay = true;
                        events.push(obj);
                        dataList.push(obj);
                    }
                });
                callback(events);
               
            });
        },
        selectable: true,
        loading: function (bool) {
            layer.load('加载中，请稍后...', 1);
        }
    });

    //设置限制为三个月以内 的时间
    function nextToMonth(button1, button2, days, type) {
        //当前时间
        var moment = $('#calendar').fullCalendar('getDate').format("YYYY-MM-DD");
        if (thistoday == "") { thistoday = moment; }

        //当日起大约或等于3个月 禁用
        var fist = new Date(thistoday);
        var next = new Date(moment);

        var fistMonth = parseInt(fist.getMonth());
        var nextMonth = parseInt(next.getMonth());

        if ((nextMonth >= (fistMonth + days) && type == "next") || (type == "prev" && nextMonth <= fistMonth)) {
            //禁用
            $(button1).addClass("fc-state-disabled");
            $(button1).attr("disabled", "disabled");

            //启用
            if (!$(button2).hasClass("fc-state-disabled")) { return false; }
            $(button2).removeClass("fc-state-disabled");
            $(button2).removeAttr("disabled");
            return false;
        }
        else {
            //启用
            if (!$(button2).hasClass("fc-state-disabled")) { return true; }
            $(button2).removeClass("fc-state-disabled");
            $(button2).removeAttr("disabled");

            return true;
        }
    }

    //设置本月 已设置的时间
    function getToDayTime() {
        var vehicleId = GetQueryStr("vehicleId");


    }
});

